package online.yysh.charging.utils;

import android.util.Log;

import com.google.gson.Gson;
import online.yysh.charging.bean.request.ApplyBackRequest;
import online.yysh.charging.base.BaseBean;
import online.yysh.charging.bean.request.BindPhoneRequest;
import online.yysh.charging.bean.request.BookingRequest;
import online.yysh.charging.bean.request.CancelApplyBackRequest;
import online.yysh.charging.bean.request.FeedbackRequest;
import online.yysh.charging.bean.request.GetAllStationsRequest;
import online.yysh.charging.bean.request.GetDeatailPortInfoRequest;
import online.yysh.charging.bean.request.GetMessageCodeRequest;
import online.yysh.charging.bean.request.GetOrderRequest;
import online.yysh.charging.bean.request.GetPreOrderRequest;
import online.yysh.charging.bean.request.GetRoundPortRequest;
import online.yysh.charging.bean.request.LoginRequest;
import online.yysh.charging.bean.request.PortIsAvailableRequest;
import online.yysh.charging.bean.request.StartChargeRequest;
import online.yysh.charging.bean.request.StopBookingRequest;
import online.yysh.charging.bean.request.StopChargingRequest;

import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class HttpUtil {
    private static HttpUtil httpUtil;
    private final String TAG = "HttpUtil";

    //获取周围枪
    private static final String URL_GET_ROUND_PORT = "https://www.sinoicar.com/GXMiniApp/Api/WX/GetRoundPort";
    //获取历史订单
    private static final String URL_GET_ORDERS = "https://www.sinoicar.com/GXMiniApp/Api/WX/charge_msg";
    //开始充电
    private static final String URL_START_CHARGING = "https://www.sinoicar.com/GXMiniApp/Api/WX/startcharge";
    //停止充电
    private static final String URL_STOP_CHARGING = "https://www.sinoicar.com/GXMiniApp/Api/WX/stopcharge";
    //申请提现
    private static final String URL_APPLY_BACK = "https://www.sinoicar.com/GXMiniApp/Api/WX/ApplyBack";
    //取消提现
    private static final String URL_CANCEL_BACK = "https://www.sinoicar.com/GXMiniApp/Api/WX/CancelBack";
    //预约充电
    private static final String URL_BOOKING = "https://www.sinoicar.com/GXMiniApp/Api/WX/booking";
    //取消预约充电
    private static final String URL_STOP_BOOKING = "https://www.sinoicar.com/GXMiniApp/Api/WX/bookingStop";
    //申请提现历史订单
    private static final String URL_APPLY_BACK_ORDERS = "https://www.sinoicar.com/GXMiniApp/Api/WX/GetApplyBack";
    //获取充值记录
    private static final String URL_GET_RECHARGE_RECORD = "https://www.sinoicar.com/GXMiniApp/Api/WX/GetRechargeRecord";
    //获取短信验证码（5分钟内有效）
    private static final String URL_GET_MESSAGE_CODE = "https://www.sinoicar.com/GXMiniApp/Api/WX/SendSMS";
    //App绑定手机号
    private static final String URL_BIND_PHONE = "https://www.sinoicar.com/GXMiniApp/Api/WX/BingPhone";
    //根据电站ID获取电站空闲桩信息（是否可以预约充电）
    private static final String URL_GET_DETAIL_PORT_INFO = "https://www.sinoicar.com/GXMiniApp/Api/WX/GetDeatailPortInfo";
    //登陆
    private static final String URL_LOGIN = "https://www.sinoicar.com/GXMiniApp/Api/WX/appAuth";
    //获取当前用户充电状态信息
    private static final String URL_AUTH_MSG = "https://www.sinoicar.com/GXMiniApp/Api/WX/auth_msg";
    //请求微信支付生成预订单
    private static final String URL_PRE_ORDER = "https://www.sinoicar.com/GXMiniApp/Api/WX/Apppreorder";
    //判断桩号是否可用
    private static final String URL_IS_AVAILABLE = "https://www.sinoicar.com/GXMiniApp/Api/WX/IsAvaiable";
    //意见反馈
    private static final String URL_FEEDBACK = "https://www.sinoicar.com/GXMiniApp/Api/WX/Feedback";
    //获取电站
    private static final String URL_GET_STATIONS = "https://www.sinoicar.com/GXMiniApp/Api/WX/GetStations";

    private static final MediaType JSON = MediaType.parse("application/json");

    /**
     * 获取电站
     * @param request
     * @param callback
     */
    public void getStations(GetAllStationsRequest getAllStationsRequest, Callback callback) {
        OkHttpClient client = new OkHttpClient();
        String json = new Gson().toJson(getAllStationsRequest);
        Log.e(TAG, "getStations: " + json);
        RequestBody requestBody = RequestBody.create(JSON, json);
        Request request = new Request.Builder().url(URL_GET_STATIONS).post(requestBody).build();
        client.newCall(request).enqueue(callback);
    }

    /**
     * 用户请求充电
     * @param startChargeRequest
     * @param callback
     */
    public void startCharge(StartChargeRequest startChargeRequest, Callback callback) {
        OkHttpClient client = new OkHttpClient();
        String json = new Gson().toJson(startChargeRequest);
        Log.e(TAG, "startCharge: " + json);
        RequestBody requestBody = RequestBody.create(JSON, json);
        Request request = new Request.Builder().url(URL_START_CHARGING).post(requestBody).build();
        client.newCall(request).enqueue(callback);
    }

    /**
     * 意见反馈
     * @param feedbackRequest
     * @param callback
     */
    public void feedback(FeedbackRequest feedbackRequest, Callback callback) {
        OkHttpClient client = new OkHttpClient();
        String json = new Gson().toJson(feedbackRequest);
        Log.e(TAG, "startCharge: " + json);
        RequestBody requestBody = RequestBody.create(JSON, json);
        Request request = new Request.Builder().url(URL_FEEDBACK).post(requestBody).build();
        client.newCall(request).enqueue(callback);
    }

    /**
     * 停止充电
     * @param stopChargingRequest
     * @param callback
     */
    public void stopCharge(StopChargingRequest stopChargingRequest, Callback callback) {
        OkHttpClient client = new OkHttpClient();
        String json = new Gson().toJson(stopChargingRequest);
        Log.e(TAG, "stopCharge: " + json);
        RequestBody requestBody = RequestBody.create(JSON, json);
        Request request = new Request.Builder().url(URL_STOP_CHARGING).post(requestBody).build();
        client.newCall(request).enqueue(callback);
    }
    /**
     * 获取提现记录
     * @param openid
     * @param callback
     */
    public void getWithdrawalRecord(String openid, Callback callback) {
        OkHttpClient client = new OkHttpClient();
        String json = new Gson().toJson(new BaseBean().setOpenid(openid));
        Log.e(TAG, "getWithdrawalRecord: " + json);
        RequestBody requestBody = RequestBody.create(JSON, json);
        Request request = new Request.Builder().url(URL_APPLY_BACK_ORDERS).post(requestBody).build();
        client.newCall(request).enqueue(callback);
    }

    /**
     * 获取充值记录
     * @param openid
     * @param callback
     */
    public void getRechargeRecord(String openid, Callback callback) {
        OkHttpClient client = new OkHttpClient();
        String json = new Gson().toJson(new BaseBean().setOpenid(openid));
        Log.e(TAG, "getRechargeRecord: " + json);
        RequestBody requestBody = RequestBody.create(JSON, json);
        Request request = new Request.Builder().url(URL_GET_RECHARGE_RECORD).post(requestBody).build();
        client.newCall(request).enqueue(callback);
    }

    /**
     * 判断桩号是否可用
     * @param portIsAvailableRequest
     * @param callback
     */
    public void isAvailable(PortIsAvailableRequest portIsAvailableRequest, Callback callback) {
        OkHttpClient client = new OkHttpClient();
        String json = new Gson().toJson(portIsAvailableRequest);
        Log.e(TAG, "isAvailable: " + json);
        RequestBody requestBody = RequestBody.create(JSON, json);
        Request request = new Request.Builder().url(URL_IS_AVAILABLE).post(requestBody).build();
        client.newCall(request).enqueue(callback);
    }

    /**
     * 向后台请求预订单
     *
     * @param getPreOrderRequest
     * @param callback
     */
    public void getPreOrder(GetPreOrderRequest getPreOrderRequest, Callback callback) {
        OkHttpClient client = new OkHttpClient();
        String json = new Gson().toJson(getPreOrderRequest);
        Log.e(TAG, "getPreOrder: " + json);
        RequestBody requestBody = RequestBody.create(JSON, json);
        Request request = new Request.Builder().url(URL_PRE_ORDER).post(requestBody).build();
        client.newCall(request).enqueue(callback);
    }


    /**
     * 登陆
     *
     * @param loginRequest
     * @param callback
     */
    public void login(LoginRequest loginRequest, Callback callback) {
        OkHttpClient client = new OkHttpClient();
        String json = new Gson().toJson(loginRequest);
        Log.e(TAG, "login: " + json);
        RequestBody requestBody = RequestBody.create(JSON, json);
        Request request = new Request.Builder().url(URL_LOGIN).post(requestBody).build();
        client.newCall(request).enqueue(callback);
    }

    private HttpUtil() {

    }

    public static HttpUtil getInstance() {
        if (httpUtil == null) {
            httpUtil = new HttpUtil();
        }
        return httpUtil;
    }

    /**
     * 根据电站ID获取电站信息（预约充电）
     * * @param stationId
     *
     * @param callback
     */
    public void getDeatailPortInfo(String stationId, Callback callback) {
        OkHttpClient client = new OkHttpClient();
        String json = new Gson().toJson(new GetDeatailPortInfoRequest(stationId));
        RequestBody requestBody = RequestBody.create(JSON, json);
        Request request = new Request.Builder().url(URL_GET_DETAIL_PORT_INFO).post(requestBody).build();
        client.newCall(request).enqueue(callback);
    }

    /**
     * 获取短信验证码
     *
     * @param phone
     */
    public void getMessageCode(String phone, Callback callback) {
        OkHttpClient client = new OkHttpClient();
        String json = new Gson().toJson(new GetMessageCodeRequest(phone));
        RequestBody requestBody = RequestBody.create(JSON, json);
        Request request = new Request.Builder().url(URL_GET_MESSAGE_CODE).post(requestBody).build();
        client.newCall(request).enqueue(callback);
    }

    /**
     * 绑定手机号
     *
     * @param bindPhoneRequest
     * @param callback
     */
    public void bindPhone(BindPhoneRequest bindPhoneRequest, Callback callback) {
        OkHttpClient client = new OkHttpClient();
        String json = new Gson().toJson(bindPhoneRequest);
        Log.e(TAG, "bindPhone: " + json);
        RequestBody requestBody = RequestBody.create(JSON, json);
        Request request = new Request.Builder().url(URL_BIND_PHONE).post(requestBody).build();
        client.newCall(request).enqueue(callback);
    }

    /**
     * 获取周围枪
     *
     * @param getRoundPortRequest
     */
    public void getRoundPort(GetRoundPortRequest getRoundPortRequest, Callback callback) {
        OkHttpClient client = new OkHttpClient();
        Gson gson = new Gson();
        String json = gson.toJson(getRoundPortRequest);
        Log.e(TAG, "getRoundPort：" + json);
        RequestBody requestBody = RequestBody.create(JSON, json);
        Request request = new Request.Builder().url(URL_GET_ROUND_PORT).post(requestBody).build();
        client.newCall(request).enqueue(callback);
    }

    /**
     * 获取历史订单
     *
     * @param openid
     * @param callback
     */
    public void getOrders(String openid, Callback callback) {
        OkHttpClient client = new OkHttpClient();
        String json = new Gson().toJson(new GetOrderRequest(openid,"1"));
        RequestBody requestBody = RequestBody.create(JSON, json);
        Request request = new Request.Builder().url(URL_GET_ORDERS).post(requestBody).build();
        client.newCall(request).enqueue(callback);
    }

    /**
     * 取消预约充电
     *
     * @param stopBookingRequest
     * @param callback
     */
    public void stopBooking(StopBookingRequest stopBookingRequest, Callback callback) {
        OkHttpClient client = new OkHttpClient();
        Gson gson = new Gson();
        String json = gson.toJson(stopBookingRequest);
        RequestBody requestBody = RequestBody.create(JSON, json);
        Request request = new Request.Builder().url(URL_STOP_BOOKING).post(requestBody).build();
        client.newCall(request).enqueue(callback);
    }

    /**
     * 预约充电
     *
     * @param bookingRequest
     * @param callback
     */
    public void booking(BookingRequest bookingRequest, Callback callback) {
        OkHttpClient client = new OkHttpClient();
        Gson gson = new Gson();
        String json = gson.toJson(bookingRequest);
        RequestBody requestBody = RequestBody.create(JSON, json);
        Request request = new Request.Builder().url(URL_BOOKING).post(requestBody).build();
        client.newCall(request).enqueue(callback);
    }

    /**
     * 取消提现
     *
     * @param callback
     */
    public void cancelApplyBack(CancelApplyBackRequest cancelApplyBackRequest, Callback callback) {
        OkHttpClient client = new OkHttpClient();
        Gson gson = new Gson();
        String json = gson.toJson(cancelApplyBackRequest);
        RequestBody requestBody = RequestBody.create(JSON, json);
        Request request = new Request.Builder().url(URL_CANCEL_BACK).post(requestBody).build();
        client.newCall(request).enqueue(callback);
    }

    /**
     * 申请提现
     *
     * @param applyBackRequest
     * @param callback
     */
    public void applyBack(ApplyBackRequest applyBackRequest, Callback callback) {
        OkHttpClient client = new OkHttpClient();
        Gson gson = new Gson();
        String json = gson.toJson(applyBackRequest);
        RequestBody requestBody = RequestBody.create(JSON, json);
        Request request = new Request.Builder().url(URL_APPLY_BACK).post(requestBody).build();
        client.newCall(request).enqueue(callback);
    }

    /**
     * 获取当前用户充电状态信息
     *
     * @param openid
     * @param callback
     */
    public void getAuthMsg(String openid, Callback callback) {
        OkHttpClient client = new OkHttpClient();
        Gson gson = new Gson();
        String json = gson.toJson(new BaseBean().setOpenid(openid));
        RequestBody requestBody = RequestBody.create(JSON, json);
        Request request = new Request.Builder().url(URL_AUTH_MSG).post(requestBody).build();
        client.newCall(request).enqueue(callback);
    }

}
